using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;

namespace IGGCore
{
    public class BaseParser
    {
        /// <summary>
        /// return the attribute in a safe way
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <param name="ifNullValue"></param>
        /// <returns></returns>
        protected string GetAttribute(XmlNode node, string name, string ifNullValue)
        {
            if (node.Attributes[name] == null)
            {
                 return ifNullValue;
            }
            else
            {
                return node.Attributes[name].InnerText;
            }
        }

        /// <summary>
        /// return the attribute in a safe way
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <param name="ifNullValue"></param>
        /// <returns></returns>
        protected int GetAttribute(XmlNode node, string name, int ifNullValue)
        {
            if (node.Attributes[name] == null)
            {
                return ifNullValue;
            }
            else
            {
                try
                {
                    return int.Parse(node.Attributes[name].InnerText);
                }
                catch
                {
                    return ifNullValue;
                }
            }
        }

        /// <summary>
        /// return the attribute in a safe way
        /// </summary>
        protected float GetAttribute(XmlNode node, string name, float ifNullValue)
        {
            if (node.Attributes[name] == null)
            {
                return ifNullValue;
            }
            else
            {
                try
                {
                    return float.Parse(node.Attributes[name].InnerText);
                }
                catch
                {
                    return ifNullValue;
                }
            }
        }

        /// <summary>
        /// return the attribute in a safe way
        /// </summary>
        /// <param name="node"></param>
        /// <param name="name"></param>
        /// <param name="ifNullValue"></param>
        /// <returns></returns>
        protected bool GetBoolAttribute(XmlNode node, string name, string condition)
        {
            if (node.Attributes[name] == null)
            {
                return false;
            }
            else
            {
                try
                {
                    string check = node.Attributes[name].InnerText;
                    return check.Equals(condition);
                }
                catch
                {
                    return false;
                }
            }
        }
    }
}
